package com.siashan.unipay4j.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.siashan.unipay4j.ali.AbstractAliPay;
import com.siashan.unipay4j.core.IPayConfig;
import com.siashan.unipay4j.core.Result;
import com.siashan.unipay4j.core.TradeStateEnum;
import com.siashan.unipay4j.core.pay.IQuery;
import com.siashan.unipay4j.core.pay.PayQueryModel;
import com.siashan.unipay4j.core.pay.PayResponse;
import com.siashan.unipay4j.core.util.EnumUtil;
import com.siashan.unipay4j.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝结果查询
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class AliPayQuery extends AbstractAliPay implements IQuery {

    public AliPayQuery(IPayConfig aliPaySet) {
        super(aliPaySet);
    }

    public AliPayQuery() {
        super();
    }


    /**
     * 方法描述:  根据外部订单号查询支付结果
     *
     * @param model   查询实体
     * @return 查询结果
     */
    @Override
    public Result<PayResponse> execQuery(PayQueryModel model) {
        log.info("AliQuery::=>订单{} 请求到达", model.getOrderNo());
        //初始化查询请求
        AlipayTradeQueryRequest alipayQueryRequest = new AlipayTradeQueryRequest();
        alipayQueryRequest.setBizContent(JSON.toJSONString(wrapParam(model)));
        //查询请求
        try {
            AlipayTradeQueryResponse r = alipayClient.execute(alipayQueryRequest);
            if (r.isSuccess()) {
                // 接口调用成功
                return Result.tradeSuccess(wrapperQueryResponse(r));
            } else {
                // 接口调用失败
                log.info("AliQuery::=>订单{} 查询失败,错误代码{},错误原因{}", model.getOrderNo(), r.getSubCode(), r.getSubMsg());
                return Result.tradeFail(r.getSubCode(), r.getSubMsg());
            }
        } catch (AlipayApiException e) {
            log.warn("AliQuery::=>订单{} 查询时发生异常", model.getOrderNo(), e);
        }
        return Result.exception();
    }

    /**
     * 封装请求参数
     *
     * @param model  请求实体
     * @return 请求参数
     */
    private Map<String, String> wrapParam(PayQueryModel model) {
        //设置请求参数
        Map<String, String> param = new HashMap<>(2);
        if (StrUtil.isNotEmpty(model.getTradeNo())) {
            param.put("trade_no", model.getTradeNo());
        }
        if (StrUtil.isNotEmpty(model.getOutTradeNo())) {
            param.put("out_trade_no", model.getOutTradeNo());
        }
        return param;
    }


    /**
     * 封装请求结果
     *
     * @param aliResponse  支付宝查询结果
     * @return 查询结果
     */
    private PayResponse wrapperQueryResponse(AlipayTradeQueryResponse aliResponse) {
        PayResponse response = new PayResponse();
        TradeStateEnum stateEnum = EnumUtil.getIEnum(AliTradeStateEnum.class, aliResponse.getTradeStatus()).getTradeState();
        response.setTradeState(stateEnum);
        response.setOutTradeNo(aliResponse.getOutTradeNo());
        response.setTradeNo(aliResponse.getTradeNo());
        response.setTotalAmount(new BigDecimal(aliResponse.getTotalAmount()));
        response.setBody(aliResponse);
        return response;
    }
}
